
const Koa=require('koa');

const Router=require('@koa/router');

const bodyParser=require('koa-bodyparser');

const app=new Koa();
const router=new Router();

// router.get('/api/list/:id',async ctx=>{//动态路由上传
//     console.log(ctx.request.params);
// });

// router.post('/api/list',async ctx=>{
//     console.log(ctx.request.body);
// });

// router.put('/api/list',async ctx=>{
//     console.log(ctx.request.body);
// });

const mysql=require('mysql');

//代码连接mysql的方式
const connection=mysql.createConnection({//创建和mysql的连接
    host     : 'localhost',
    user     : 'root',
    password : '123456',
    port:'3306',
    database : 'my_user'
});

connection.connect();//建立连接



router.post('/api/login',async ctx=>{
console.log(ctx.request.query);
    const {username,pwd}=ctx.request.body;

    let sql=`select * from login where username='${username}' and pwd='${pwd}'`;

    connection.query(sql,(err,result)=>{
        if(err) throw err;
        console.log(result);
    })
})
app.use(bodyParser());
app.use(router.routes());

app.listen(9000,()=>{
    console.log(`service is running at http://localhost:9000`);
})